package com.alex.controller;


import cn.hutool.http.HttpUtil;
import com.alex.entityVo.NsfcSearchVo;
import com.alex.service.CommonService;
import com.alex.service.StaticService;
import com.alex.vo.Nsfc;
import com.alex.vo.NsfcMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import common.beans.ResultBean;
import common.util.CheckUtil;
import common.util.StringUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/nsfc")
@SuppressWarnings(value = "all")
@Slf4j
public class NsfcController {

    //统一检索的jar包
    public static String RESTURL = "http://10.1.1.11:65213/";

    @Resource
    CommonService commonService;

    @Resource
    NsfcMapper nsfcMapper;

    @Resource
    StaticService staticService;

    public static String getAllSearch(NsfcSearchVo nsfcSearchVo) {

        //   //标题
        //    private String title;
        //    //依托单位
        //    private String organ;
        //    // 负责人
        //    private String charge;
        //    // 项目类型
        //    private String type;
        //    // 批准年份
        //    private String ratifyyear;
        //    // 学科小类
        //    private String subject;
        //    //学科大类
        //    private String topsubject;

        String result = "";
        if (StringUtil.isNotNull(nsfcSearchVo.getTitle())) {
            result += "标题:" + nsfcSearchVo.getTitle() + " ";
        }
        if (StringUtil.isNotNull(nsfcSearchVo.getOrgan())) {
            result += "依托单位:" + nsfcSearchVo.getOrgan() + " ";
        }
        if (StringUtil.isNotNull(nsfcSearchVo.getCharge())) {
            result += "负责人:" + nsfcSearchVo.getCharge() + " ";
        }
        if (StringUtil.isNotNull(nsfcSearchVo.getType())) {
            result += "项目类型:" + nsfcSearchVo.getType() + " ";
        }
        if (StringUtil.isNotNull(nsfcSearchVo.getRatifyyear())) {
            result += "批准年份:" + nsfcSearchVo.getRatifyyear() + " ";
        }
        if (StringUtil.isNotNull(nsfcSearchVo.getSubject())) {
            result += "学科小类:" + nsfcSearchVo.getSubject() + " ";
        }
        if (StringUtil.isNotNull(nsfcSearchVo.getTopsubject())) {
            result += "学科大类:" + nsfcSearchVo.getTopsubject() + " ";
        }
        return result;
    }

    @PostMapping(value = "search")
    public ResultBean<Object> auth_article(@RequestBody NsfcSearchVo nsfcSearchVo) throws Exception {

        String allSearch = getAllSearch(nsfcSearchVo);
        staticService.insStat(allSearch);
        String result = HttpUtil.post(RESTURL + "nsfc/search2", JSONObject.toJSONString(nsfcSearchVo));
        return JSONObject.parseObject(result, ResultBean.class);
    }

    @GetMapping(value = "search-detail")
    public ResultBean<Object> nsfcDetail(String nfscno) throws Exception {
        CheckUtil.check(StringUtil.isNotNull(nfscno), "基金号不能空");
        List<Nsfc> var3 = nsfcMapper.selectList(new QueryWrapper<Nsfc>().eq("id", nfscno));
        if (var3.size() > 0) {
            Nsfc nsfc = var3.get(0);
            staticService.insStat(nsfc.getVar2());
            commonService.dealSpNsfc(nsfc);
            return new ResultBean<>(nsfc);
        } else {
            CheckUtil.check(false, "检索错误");
        }

        return new ResultBean<>();
    }
}


















































